//
//  CZTabBar.swift
//  Weibo24
//
//  Created by apple on 16/2/24.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

/*
    如何才能自定义
    1. 修改 TabBar里子控件的frame
    2. 修改frame之后可以添加一个button


    问题:
    1.在哪里去修改frame -> layoutSubviews
    2.按钮的位置

*/

class CZTabBar: UITabBar {

    
    
    //自定义视图三部曲
    //第一步: 重写 init
    override init(frame: CGRect) {
        super.init(frame: frame)
        //来添加视图和设置约束
        setupUI()
    }
    //自定义了一个视图 会走init(frame: CGRect)
    //系统怕你采用storyboard ,会给你一个提示
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //第二部:创建一个私有方法,来添加视图和设置约束
    private func setupUI(){
    
        addSubview(composeButton)
        
    }
    
    
    //第三部: 懒加载
    //MARK:--懒加载
    lazy var composeButton: UIButton = {
    
        let buttton = UIButton(type: UIButtonType.Custom)
        
        // 设置按钮的属性
        buttton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        buttton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        buttton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        buttton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //可以添加点击事件
        
        //设置大小
        buttton.sizeToFit()
        
        return buttton
    }()
    
    
   //在这里去修改
    override func layoutSubviews() {
    
        //先调用super.layoutSubviews
        super.layoutSubviews()
        /*
        <_UITabBarBackgroundView: 0x7fd0bba9e660; frame = (0 0; 414 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fd0bba186c0>>, 
        <UITabBarButton: 0x7fd0b9c22740; frame = (2 1; 100 48); opaque = NO; layer = <CALayer: 0x7fd0b9c24340>>, 
        <UITabBarButton: 0x7fd0bba981f0; frame = (106 1; 99 48); opaque = NO; layer = <CALayer: 0x7fd0bba98130>>, 
        <UITabBarButton: 0x7fd0bbbda590; frame = (209 1; 100 48); opaque = NO; layer = <CALayer: 0x7fd0bbb9cae0>>, 
        <UITabBarButton: 0x7fd0bbbdaf70; frame = (313 1; 99 48); opaque = NO; layer = <CALayer: 0x7fd0bbbdca90>>, 
        <UIImageView: 0x7fd0bba9c4e0; frame = (0 -0.333333; 414 0.333333); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fd0bba9d140>>]
        */
        //UITabBarButton 属于苹果私有的
        //创建一个索引
        var index = 0
        let width = bounds.width / 5
        let height = bounds.height
        
        
        for subview in subviews{
            
            //判断一下 subview 是不是 UITabBarButton
            //如果是 UITabBarButton 可以修改frame
            
            // String可以转类
            //NSClassFromString("UITabBarButton")返回了一个可选项
            if  subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
//                print("UITabBarButton")
                //修改frame
                subview.frame = CGRectMake(CGFloat (index++) * width, 0, width, height)
                //判断一下索引
                if index == 2 {index++}
            }
            
        }
        
        
        //设置按钮的位置
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
//        print(subviews)
//        print("=======")
    }
    
}
